﻿Imports ATS.Business.Entities
Imports ATS.Data
Imports System.Transactions


Public Class AccountPolicyComponent
#Region "CRUD"
    Public Function CreateNewAccountPolicy(ByVal accountPolicyValues As admPasswordConfig) As admPasswordConfig
        Console.WriteLine("Saving... ")
        Using ts As New TransactionScope(TransactionScopeOption.Required)
            Try
                accountPolicyValues = CreateAccountPolicy(accountPolicyValues)
                ts.Complete()
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Throw ex
            End Try
        End Using

        Return accountPolicyValues
    End Function

    Public Function AccountPolicyValues() As admPasswordConfig
        ' Retrieve data.
        Dim dac As New AssetDataAccess()
        Try
            Dim admPassCfg = dac.GetPasswordConfigDetails
            If admPassCfg Is Nothing Then
                Dim accountPolicyValue As New admPasswordConfig
                Return CreateNewAccountPolicy(accountPolicyValue)
            Else
                Return admPassCfg
            End If
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Function

    Public Function UpdateAccountPolicyValues(ByVal accountPolicyValues As admPasswordConfig) As admPasswordConfig
        Return UpdateAccountPolicyRecord(accountPolicyValues)
    End Function
#End Region


#Region "Support Procedures"
    Private Function CreateAccountPolicy(ByVal accountPolicyValues As admPasswordConfig) As admPasswordConfig
        ' Business logic
        With accountPolicyValues
            .PWLength = 0
            .PWExpiration = 0
            .PWFormat1 = 0
            .PWFormat2 = 0
            .PWFormat3 = 0
            .PWLockAccount = 0
            .PWDifferentFromID = 1
        End With

        ' Persist data.
        Dim dac As New AssetDataAccess()
        Try
            Return dac.CreatePasswordConfigDetails(accountPolicyValues)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Function

    Private Function UpdateAccountPolicyRecord(ByVal accountPolicyValues As admPasswordConfig) As admPasswordConfig
        Using ts As New TransactionScope(TransactionScopeOption.Required)
            Try
                accountPolicyValues = UpdateAccountPolicy(accountPolicyValues)
                ts.Complete()
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Throw ex
            End Try
        End Using

        Return accountPolicyValues
    End Function

    Private Function UpdateAccountPolicy(ByVal accountPolicyValues As admPasswordConfig) As admPasswordConfig
        ' Persist data.
        Dim dac As New AssetDataAccess()
        Try
            dac.UpdatePasswordConfigDetails(accountPolicyValues)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try

        Return accountPolicyValues
    End Function
#End Region


End Class
